package com.bootdo.market.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.market.dao.DuplicateDao;
import com.bootdo.market.domain.ProductTagsDO;
import com.bootdo.market.service.ProductTagsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author jinse
 * @email market@jinse.com
 * @date 2019-04-16 11:51:47
 */

@Controller
@RequestMapping("/market/productTags")
public class ProductTagsController {
    @Autowired
    private ProductTagsService productTagsService;
    @Autowired
    private DuplicateDao duplicateDao;

    @GetMapping()
    @RequiresPermissions("market:productTags:productTagsList")
    String productTagsList() {
        return "market/productTags/productTagsList";
    }

    @ResponseBody
    @GetMapping("/productTagsListlist")
    @RequiresPermissions("market:productTags:productTagsListlist")
    public PageUtils productTagsListlist(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ProductTagsDO> productTagsList = productTagsService.productTagsListlist(query);
        int total = productTagsService.productTagsListlistCount(query);
        PageUtils pageUtils = new PageUtils(productTagsList, total);
        return pageUtils;
    }

    @GetMapping("/ProductTags")
    @RequiresPermissions("market:productTags:productTags")
    String ProductTags(Integer productid, Model model) {
        model.addAttribute("productid", productid);
        return "market/productTags/productTags";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:productTags:productTags")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ProductTagsDO> productTagsList = productTagsService.list(query);
        int total = productTagsService.count(query);
        PageUtils pageUtils = new PageUtils(productTagsList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("market:productTags:add")
    String add(Integer productid, Model model) {
        model.addAttribute("productid", productid);
        return "market/productTags/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:productTags:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        ProductTagsDO productTags = productTagsService.get(id);
        model.addAttribute("productTags", productTags);
        return "market/productTags/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:productTags:add")
    public R save(ProductTagsDO productTags) {
        if (productTagsService.save(productTags) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("market:productTags:edit")
    public R update(ProductTagsDO productTags) {
        productTagsService.update(productTags);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:productTags:remove")
    public R remove(ProductTagsDO productTags) {
        productTagsService.update(productTags);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:productTags:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            ProductTagsDO productTags = new ProductTagsDO();
            productTags.setId(ids[i]);
            productTags.setTagonlinestate(1);
            productTagsService.update(productTags);
        }
        return R.ok();
    }

    @RequestMapping(value = "/productTagsJudgingUniqueAdd", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productTagsJudgingUniqueAdd(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productTagsJudgingUniqueAdd(params);
        if (aa == null) {
            Map<String, Object> bb = duplicateDao.tagNameproductTagsJudgingUniqueAdd(params);
            if (bb == null) {
                return R.ok();
            } else {
                return R.error();
            }
        } else {
            return R.error();
        }
    }

    @RequestMapping(value = "/productTagsJudgingUniqueEdit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productTagsJudgingUniqueEdit(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productTagsJudgingUniqueEdit(params);
        if (aa == null) {
            Map<String, Object> bb = duplicateDao.tagNameproductTagsJudgingUniqueEdit(params);
            if (bb == null) {
                return R.ok();
            } else {
                return R.error();
            }
        } else {
            return R.error();
        }
    }
}
